package com.huifaapp.mall.activity.Homefragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.MessagetwoAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.MessagetwoBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.ToastUtil;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

public class MessagetwoActivity extends AppCompatActivity {

    private XRecyclerView recyclerView;
    private MessagetwoAdapter messagetwoAdapter;
    private List<MessagetwoBean.DataBean.ListBean>list;
    String id;
    int curr;
    private ImageView fanhui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messagetwo);
        initView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initView() {
        recyclerView = (XRecyclerView) findViewById(R.id.messagetwo_recy);
        fanhui = (ImageView) findViewById(R.id.twotwo_back);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        id = intent.getStringExtra("parentId");
        list = new ArrayList<>();
        requestEmit();
        messagetwoAdapter = new MessagetwoAdapter(MessagetwoActivity.this,list);
        recyclerView.setAdapter(messagetwoAdapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curr = 0;
                requestEmit();
            }

            @Override
            public void onLoadMore() {
                curr++;
                requestEmit();
            }
        });
        requestEmit();
    }

//        OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"msg/list")
//                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
//                .addParams("appId", AUniqueIdentifier.getDeviceId(MessagetwoActivity.this))
//                .addParams("parentId",id)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Request request, Exception e) {
//                    }
//                    @Override
//                    public void onResponse(String response) {
//
//                        try {
//                            Gson gson = new Gson();
//                            MessagetwoBean messagetwoBean = gson.fromJson(response,MessagetwoBean.class);
//                            list = messagetwoBean.getData().getList();
//                            messagetwoAdapter = new MessagetwoAdapter(MessagetwoActivity.this,list);
//                            recyclerView.setAdapter(messagetwoAdapter);
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
//                    }
//                });}

    private void requestEmit(){
        System.out.println("response curr = " + curr);
        OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"msg/list")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(MessagetwoActivity.this))
                .addParams("start",curr+"")
                .addParams("length",10+"")
                .addParams("parentId",id)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {}
                    @Override
                    public void onResponse(String response) {
//                        try {
//                            Gson gson = new Gson();
//                            MessagetwoBean messagetwoBean = gson.fromJson(response,MessagetwoBean.class);
//                            list = messagetwoBean.getData().getList();
//                            messagetwoAdapter = new MessagetwoAdapter(MessagetwoActivity.this,list);
//                            recyclerView.setAdapter(messagetwoAdapter);
//
//                        }catch (Exception e){
//                            e.printStackTrace();
//                        }
                        recyclerView.refreshComplete();
                        initData(response);
                    }
                });
    }

    private void initData(String response) {
        Gson gson = new Gson();
        MessagetwoBean messagetwoBean = gson.fromJson(response,MessagetwoBean.class);
        if (messagetwoBean != null && messagetwoBean.getData() != null && messagetwoBean.getData().getList() != null) {
            if (curr == 0) {
                list.clear();
            }
            list.addAll(messagetwoBean.getData().getList());
            messagetwoAdapter.notifyDataSetChanged();
        } else {
            ToastUtil.showL(MessagetwoActivity.this, "data is error");
        }
    }

}
